package com.warm.netty;//package com.hh.nettydemo.server;

import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@Component
@WebListener
public class HmpsOrionTCPListener implements ServletContextListener {
    private static Logger logger = LoggerFactory.getLogger(HmpsOrionTCPListener.class);

    private static int Port = 11111;

    @Autowired
    private ThreadConfig threadConfig;

    @SneakyThrows
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        logger.info("======== 启动一个线程，监听设备传输数据包，监听端口：{} ======== ", Port);
        // 启动一个线程，监听数据报
        threadConfig.taskExecutor().execute(() -> {
            // 开启Netty服务
            NettyServer nettyServer = new NettyServer();
            nettyServer.start();
        });
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        logger.info("======== 关闭监听数据包 ======== ", Port);
    }

    public static int getPort() {
        return Port;
    }

    public static void setPort(int Port) {
        HmpsOrionTCPListener.Port = Port;
    }
}
